package com.yusp75.myexam.ui.exam

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.fragment.findNavController
import com.yusp75.myexam.R
import com.yusp75.myexam.databinding.FragmentExamResultBinding
import org.koin.androidx.viewmodel.ext.android.viewModel

class ExamResultFragment : Fragment() {
    private val viewModel: ExamResultViewModel by viewModel()
    private lateinit var binding: FragmentExamResultBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentExamResultBinding.inflate(inflater)
        return binding.root
    }

    @SuppressLint("SetTextI18n")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        (activity as? AppCompatActivity)?.supportActionBar?.setDisplayHomeAsUpEnabled(false)

        val adapter = ExamResultAdapter { /* 处理点击事件 */ }
        binding.examQuestions.adapter = adapter

        viewModel.failureQuestions.observe(viewLifecycleOwner) { questions ->
            questions?.takeIf { it.isNotEmpty() }?.let {
                adapter.submitList(it)
            }
        }

        // 结果统计
        binding.score.text = viewModel.scoreTxt
        binding.elapsedTime.text = viewModel.totalTimeTxt

        // 确定按钮
        binding.btnConfirm.setOnClickListener {
            findNavController().navigate(R.id.nav_home)
        }
    }

    //
}
